<?php
namespace app\route;

use think\Route;
use think\Config;
use think\Db;
use think\Request;

if (!is_file(PUBLIC_PATH . 'install' . DS .'install.lock')) {
    if (BAIDU) {
        Route::rule('/','install/BchInstall/index');
    } else {
        Route::rule('/','install/Install/index');
        Route::rule('/install','install/Install/index');
        Route::rule('/install/installPost','install/Install/installPost');
        Route::rule('/install/installPostOne','install/Install/installPostOne');
    }
} else {
	$request = Request::instance();  
    $subSite = false;
    if (strpos($request->server()['REQUEST_URI'],'/subSite/') !== false) {
    	$subSite = true;
    } else {
    	try {
			$httpDomain = $request->domain();
			$domain = str_replace('http://', '', $httpDomain);
			$domain = str_replace('https://', '', $domain);
			$subSiteInfo = Db::connect([], '')->name('Sites')->where('domain',$domain)->find();
		    config('subSiteInfo',$subSiteInfo);
	    } catch (\Exception $e) {}
    }
    if ($subSiteInfo || $subSite) {
    	try {
	    	$header = Request::instance()->header();
		    if (isset($header['dataid']) && $header['dataid'] && ($header['dataid'] != 'undefined')) {
				$subSiteInfo = Db::connect([], '')->name('Sites')->where('id',$header['dataid'])->find();
			    config('subSiteInfo',$subSiteInfo);
			}
		    if (is_file(APP_PATH . 'subSite' . DS . 'siteRoute.php')) {
		        require APP_PATH . 'subSite' . DS . 'siteRoute.php';
		    }
	    } catch (\Exception $e) {}
    } else {
	    //如果修改系统管理地址，请修改后一个admin即可
	    config('admin','admin');
	    
	    if (is_file(APP_PATH . 'domianInit.php')) {
	        require APP_PATH . 'domianInit.php';
	    }
	    
	    if (is_dir(ROOT_PATH . 'addons' . DS)) {
	        foreach (fetch_dir(ROOT_PATH . 'addons' . DS) as $key => $dir) {
	            if (is_file($dir . DS . 'route.php')) {
	                require $dir . DS . 'route.php';
	            }
	        }
	    }   
	    
	    if (!strpos($request->url(),'Api')) {
	            Route::group(config('admin'), [
	            '/' => 'admin/Admin/index',
	            '/login' => 'admin/Login/index',
	            '/componentjs' => 'admin/Admin/component',
	            '/componentcss' => 'admin/Admin/componentcss',
	            '/addons/addonsjs' => 'admin/Admin/addonsJs',
	            '/addons/addonscss' => 'admin/Admin/addonsCss',
	        ],[],[]);
	    }
	    
	    foreach (fetch_dir(APP_PATH) as $key => $dir) {
	        if (is_file($dir . DS . 'route.php')) {
	            require $dir . DS . 'route.php';
	        }
	    }
    }
}